package com.haochang.chunkvod.controller.activity.hostess;

import android.view.View;
import android.widget.LinearLayout;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseGridView;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.tools.vod.log.Logger;
import com.haochang.chunkvod.tools.vod.other.GridViewUtil;
import com.haochang.chunkvod.view.vod.title.TitleView;

import java.util.ArrayList;

/**
 *   created at 2016/8/29 10:27
 *   咨客 提醒页面
 */
public class RemindActivity extends BaseActivity
{
	private LinearLayout group_call_remind, group_statement_remind, group_over_remind;
	private BaseGridView grid_remind_call;
	private BaseListView list_remind_statement, list_remind_over;

	private ArrayList<HostessInfo> listCalling = new ArrayList<HostessInfo>();//呼叫提醒
	private ArrayList<HostessInfo> listReservation = new ArrayList<HostessInfo>();//预结提醒
	private ArrayList<HostessInfo> listOverTime = new ArrayList<HostessInfo>();//超时提醒

	private QuickAdapter<HostessInfo> adapterReservation;
	private HostessData hostessData;

	@Override
	public void receiveParam()
	{

	}

	@Override
	public void initData()
	{
		hostessData = new HostessData(this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_remind);
		group_call_remind = (LinearLayout) findViewById(R.id.group_call_remind);
		group_statement_remind = (LinearLayout) findViewById(R.id.group_statement_remind);
		group_over_remind = (LinearLayout) findViewById(R.id.group_over_remind);
		grid_remind_call = (BaseGridView) findViewById(R.id.grid_remind_call);
		list_remind_statement = (BaseListView) findViewById(R.id.list_remind_statement);
		list_remind_over = (BaseListView) findViewById(R.id.list_remind_over);
		TitleView titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle("提醒").setRightIconVISIBLE(false).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
				animRightToLeft();
			}

			@Override
			public void onDoneClick()
			{

			}

		});
	}

	@Override
	public void onResume()
	{
		super.onResume();
		requestNotification();
	}

	/**
	 * 呼叫提醒列表
	 */
	private void callingList()
	{
		QuickAdapter<HostessInfo> adapterCallling = new QuickAdapter<HostessInfo>(this, R.layout.item_reserve_room_layout, listCalling)
		{
			@Override
			protected void convert(BaseAdapterHelper helper, HostessInfo item)
			{
				helper.getView().setBackgroundColor(getResources().getColor(R.color.white));
				final BaseTextView tv_left = helper.getView(R.id.tv_left);
				final BaseTextView tv_right = helper.getView(R.id.tv_right);
				final View spaceLineTop = helper.getView(R.id.spaceLineTop);
				tv_left.setText(item.getRoomNum());
				tv_right.setText(item.getStart_time());
				if (helper.getPosition() == 0 || helper.getPosition() == 1)
					spaceLineTop.setVisibility(View.VISIBLE);
				else
					spaceLineTop.setVisibility(View.GONE);
				int time;
				try
				{
					time = Integer.parseInt(item.getTime().trim());
					if (time >= 0 && time < 2)
					{
						tv_left.setTextColor(getResources().getColor(R.color.color_282828));
						tv_right.setTextColor(getResources().getColor(R.color.color_282828));
					} else if (time < 5)
					{
						tv_left.setTextColor(getResources().getColor(R.color.color_ffab40));
						tv_right.setTextColor(getResources().getColor(R.color.color_ffab40));
					} else if (time >= 5)
					{
						tv_left.setTextColor(getResources().getColor(R.color.color_f04e39));
						tv_right.setTextColor(getResources().getColor(R.color.color_f04e39));
					}
				} catch (Exception e)
				{
					Logger.e("呼叫提醒列表:" + e.getMessage());
				}
			}
		};
		grid_remind_call.setAdapter(adapterCallling);
		GridViewUtil.setListViewHeightBasedOnChildren(this, grid_remind_call,true);
		adapterCallling.notifyDataSetChanged();
	}

	/**
	 * 预结提醒列表
	 */
	private void reservationTimeList()
	{
		adapterReservation = new QuickAdapter<HostessInfo>(this, R.layout.item_remind_statement, listReservation)
		{
			@Override
			protected void convert(BaseAdapterHelper helper, final HostessInfo item)
			{
				helper.setText(R.id.tv_room_num, item.getRoomNum());
				helper.setText(R.id.tv_statement_time, item.getStatementTime());
				helper.setText(R.id.tv_remain_time, item.getRemainTime() + "分钟");
				helper.getView().findViewById(R.id.remove).setTag(item);
				helper.setOnClickListener(R.id.remove, onRemoveClickedListener);
			}
		};
		list_remind_statement.setAdapter(adapterReservation);
	}

	OnBaseClickListener onRemoveClickedListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			HostessInfo item = (HostessInfo) v.getTag();
			listReservation.remove(item);
			if (listReservation.size() > 0)
				adapterReservation.replaceAll(listReservation);
			else
				group_statement_remind.setVisibility(View.GONE);
		}
	};

	/**
	 * 超时提醒列表
	 */
	private void timeOverList()
	{
		QuickAdapter<HostessInfo> adapterOverTime = new QuickAdapter<HostessInfo>(this, R.layout.item_remind_over, listOverTime)
		{
			@Override
			protected void convert(BaseAdapterHelper helper, HostessInfo item)
			{
				helper.setText(R.id.tv_room_num, item.getRoomNum());
				helper.setText(R.id.tv_over_type, item.getOverType());
				String str;
				try
				{
					int time = Integer.parseInt(item.getOverTime());
					if (time >= 60)
						str = time / 60 + "小时" + time % 60 + "分钟";
					else
						str = time + "分钟";
					helper.setText(R.id.tv_over_time, str);
				} catch (Exception e)
				{
					e.printStackTrace();
					helper.setText(R.id.tv_over_time, "0分钟");
				}
			}
		};
		list_remind_over.setAdapter(adapterOverTime);
	}

	/**
	 * 请求提醒列表
	 */
	private void requestNotification()
	{
		hostessData.requestNotification(new HostessData.NotificationListener()
		{
			@Override
			public void onRequestSuccess(long serverTime, ArrayList<HostessInfo> listCalling, ArrayList<HostessInfo> listReservation,
					ArrayList<HostessInfo> listOverTime)
			{
				if (listCalling != null)
					RemindActivity.this.listCalling = listCalling;
				if (listReservation != null)
					RemindActivity.this.listReservation = listReservation;
				if (listOverTime != null)
					RemindActivity.this.listOverTime = listOverTime;
				if (listCalling != null && listCalling.size() > 0)
				{
					group_call_remind.setVisibility(View.VISIBLE);
					callingList();
				} else
					group_call_remind.setVisibility(View.GONE);
				if (listReservation != null && listReservation.size() > 0)
				{
					group_statement_remind.setVisibility(View.VISIBLE);
					reservationTimeList();
				} else
					group_statement_remind.setVisibility(View.GONE);
				if (listOverTime != null && listOverTime.size() > 0)
				{
					group_over_remind.setVisibility(View.VISIBLE);
					timeOverList();
				} else
					group_over_remind.setVisibility(View.GONE);
			}

			@Override
			public void onFail(int errno, String errMsg)
			{

			}
		});
	}

}
